
#include <iostream>
#include <memory>
#include <functional>
#include <unordered_map>
#include "TcpServer.hpp"
#include "Protocol.hpp"

using namespace std;

void Calculate(my_protocol::Request &req, my_protocol::Response* resp)
{
    // 这里只是简单的计算而已
    int x = req._x;
    int y = req._y;
    char op = req._op;
    unordered_map<char, function<int()>> hash =
        {
            {'+', [&]()
             { return x + y; }},
            {'-', [&]()
             { return x - y; }},
            {'*', [&]()
             { return x * y; }},
            {'/', [&]()
             {
                 if (y == 0)
                 {
                     resp->_code = 1;
                     return 0;
                 }

                 resp->_code = 0;
                 return x / y;
             }}};

    if (hash.count(op) == 0)
        resp->_code = 3;
    else
        resp->_result = hash[op]();
}
int main()
{
    unique_ptr<CalcServer::TcpServer> tsvr(new CalcServer::TcpServer(Calculate));

    tsvr->Init();
    tsvr->Start();

    return 0;
}